#ifndef WINVER                          // Specifies that the minimum required platform is Windows Vista.
#define WINVER 0x0600           // Change this to the appropriate value to target other versions of Windows.
#endif

#ifndef _WIN32_WINNT            // Specifies that the minimum required platform is Windows Vista.
#define _WIN32_WINNT 0x0600     // Change this to the appropriate value to target other versions of Windows.
#endif

#ifndef _WIN32_WINDOWS          // Specifies that the minimum required platform is Windows 98.
#define _WIN32_WINDOWS 0x0410 // Change this to the appropriate value to target Windows Me or later.
#endif

#ifndef _WIN32_IE                       // Specifies that the minimum required platform is Internet Explorer 7.0.
#define _WIN32_IE 0x0700        // Change this to the appropriate value to target other versions of IE.
#endif

#define WIN32_LEAN_AND_MEAN             // Exclude rarely-used stuff from Windows headers
#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS      // some CString constructors will be explicit

#ifndef VC_EXTRALEAN
#define VC_EXTRALEAN            // Exclude rarely-used stuff from Windows headers
#endif

#include <afx.h>
#include <afxwin.h>         // MFC core and standard components
#include <afxext.h>

#include <strsafe.h>
#include "XmlHelper.h"

void SplitESGXML(const CString & xml, CStringArray & schedules)
{
	const CString & pattern = _T("<?xml ");
	int nStart = 0;

	while(true)
	{
		int nBegin = xml.Find(pattern, nStart);

		if (nBegin < 0)
			return;

		int nEnd = xml.Find(pattern, nBegin + pattern.GetLength());

		if (nEnd < 0)
		{
			schedules.Add(xml.Mid(nBegin));

			return;
		}
		else
		{
			schedules.Add(xml.Mid(nBegin, nEnd - nBegin));

			nStart = nEnd;
		}
	}
}

TCHAR * TrimAll(const TCHAR * pszData, const size_t cch)
{
	if (pszData == NULL || cch == 0 || cch == 1)
		return new TCHAR[1];

	size_t new_cch = 0;

	size_t begin = 0;

	for(begin = 0;begin < cch; begin++)
	{
		if (pszData[begin] != _T(' ') &&
			pszData[begin] != _T('\t') &&
			pszData[begin] != _T('\r') &&
			pszData[begin] != _T('\n'))
		{
			break;
		}
	}

	if (begin == cch)
		return new TCHAR[1];

	size_t end = cch - 1 - 1;

	for(end = cch - 1 - 1; end > begin; end --)
	{
		if (pszData[end] != _T(' ') &&
			pszData[end] != _T('\t') &&
			pszData[end] != _T('\r') &&
			pszData[end] != _T('\n'))
		{
			break;
		}
	}

	new_cch = end - begin + 1 + 1;

	TCHAR * pszNew = new TCHAR[new_cch];

	StringCchCopy(pszNew, new_cch, &pszData[begin]);

	return pszNew;
}

CString RemoveXmlHeader(const CString & strContent)
{
	int nBegin = strContent.Find(_T("<?xml "));

	if (nBegin < 0)
		return strContent;

	int nEnd = strContent.Find(_T("?>"), nBegin + 1);

	if (nEnd < 0)
		return strContent;

	return strContent.Mid(nEnd + 2);
}
